package com.sg.zhui.visitor.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sg.zhui.visitor.R;
import com.sg.zhui.visitor.http.AsyncRequest;
import com.sg.zhui.visitor.http.BaseStringCallback_Host;
import com.sg.zhui.visitor.http.Setting;
import com.sg.zhui.visitor.http.httpContxt;
import com.sg.zhui.visitor.utils.PreferencesHelper;
import com.sg.zhui.visitor.utils.StringUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

//import butterknife.BindView;
//import butterknife.ButterKnife;
//import butterknife.OnClick;
//import butterknife.Unbinder;
/**
 * Created by 李 on 2018/4/18.
 * 我的任务三个页面
 */

public class HomeFragment extends Fragment implements AsyncRequest {
//    @BindView(R.id.view_lines)
//    View viewLines;
//    @BindView(R.id.tv_shop_business)
//    TextView tvShopBusiness;
//    @BindView(R.id.rl_apply_visit_code)
//    RelativeLayout rlApplyVisitCode;
//    @BindView(R.id.container)
//    LinearLayout container;
//    Unbinder unbinder;
    private PreferencesHelper preferencesHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View view = inflater.inflate(R.layout.home_fragment_layout, container, false);
//        unbinder = ButterKnife.bind(this, view);

        preferencesHelper = new PreferencesHelper(getActivity());
        return view;
    }

//    @OnClick({R.id.rl_apply_visit_code})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.rl_apply_visit_code:
//                verificationCode();
////                queryUserInfo();
//                break;
//
//        }
//    }

    /**
     * 验证码
     */
    public void verificationCode() {
        String url = Setting.sendVerificationUrl();

        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("userTel", "18210182462");
        stringStringHashMap.put("userName", "陈立朋");

        OkHttpUtils
                .post().params(stringStringHashMap)
                .url(url)
                .id(httpContxt.SEND_VERIFICATION_URL)
                .build()
                .execute(new BaseStringCallback_Host(getActivity(), this));
    }


    /**
     * 用户信息
     */
    public void queryUserInfo() {

        String url = Setting.queryUserInfo();

        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("userId", "135964958e9e4f9b8b92ededa16e4dbe");
        stringStringHashMap.put("userType", "0");

        OkHttpUtils
                .post().params(stringStringHashMap)
                .url(url)
                .id(httpContxt.QUERY_USER_INFO)
                .build()
                .execute(new BaseStringCallback_Host(getActivity(), this));
    }


    @Override
    public void RequestComplete(Object object, Object data) {
        Log.e("数据RequestComplete", data.toString());
//        {
//            "userId" : "135964958e9e4f9b8b92ededa16e4dbe",
//                "type" : "0",
//                "state" : true,
//                "msg" : "验证码已发送，请及时查收.",
//                "verification" : "1423"
//        }

//        {
//            "code" : 1000,
//                "msg" : null,
//                "data" : {
//            "userId" : "135964958e9e4f9b8b92ededa16e4dbe",
//                    "userTel" : "18210182462",
//                    "userName" : "123",
//                    "cardNo" : "",
//                    "surplusTime" : "0",
//                    "isToday" : "到期",
//                    "photo" : null,
//                    "carNum" : null,
//                    "companyName" : null
//        }
//        }
        if (object.equals(httpContxt.SEND_VERIFICATION_URL)) {//验证码

            try {
                JSONObject jsonObject = new JSONObject((String) data);
//                JSONObject data1 = jsonObject.getJSONObject("data");
                String userId = jsonObject.getString("userId");

                if (!StringUtil.isEmpty(userId)) {
                    preferencesHelper.saveUserId(userId);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


        } else if (object.equals("2")) {

        }
    }

    @Override
    public void RequestError(Object object, int errorId, String errorMessage) {
        Log.e("数据RequestError", errorMessage);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        unbinder.unbind();
    }
}
